package dnl.toolbox.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dnl.toolbox.lang.ClassPathHacker;

public class ClasspathLoader {

	private static Logger logger = LoggerFactory.getLogger(ClasspathLoader.class);

	public void loadClasspathFromConfig() {
		InputStream is = Config.getValireResource("resources/classpath.config");
		if (is == null)
			return;
		loadClasspathImpl(is);
	}

	public void loadClasspathFromStream(InputStream is) {
		loadClasspathImpl(is);
	}

	private void loadClasspathImpl(InputStream is) {
		try {
			Properties properties = new Properties();
			properties.load(is);
			Collection<Object> values = properties.values();
			StringBuilder sb = new StringBuilder();
			for (Object object : values) {
				sb.append(object.toString());
				sb.append("\n");
			}
			logger.info("Loading additional class path entries: \n{}", sb.toString());
			ClassPathHacker.load(properties);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

}
